package com.ruoyi.project.bus.register.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.bus.register.domain.BusRegister;
import com.ruoyi.project.bus.register.service.IBusRegisterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 登录信息增删改查Controller
 * 
 * @author ruoyi
 * @date 2020-11-26
 */
@Controller
@RequestMapping("/register/register")
public class BusRegisterController extends BaseController
{
    private String prefix = "bus/register";

    @Autowired
    private IBusRegisterService busRegisterService;

    @RequiresPermissions("bus:register:view")
    @GetMapping("{type}")
    public String register(@PathVariable("type") String type, ModelMap map)
    {
        map.put("type", type);
        return prefix + "/register";
    }

    @GetMapping("/query/{type}")
    public String registerQuery(@PathVariable("type") String type, ModelMap map)
    {
        map.put("type", type);
        return prefix + "/register_query";
    }

    /**
     * 查询登录信息增删改查列表
     */
    @RequiresPermissions("bus:register:list")
    @PostMapping("/list/{type}")
    @ResponseBody
    public TableDataInfo list(BusRegister busRegister, @PathVariable("type") String type)
    {
        startPage();
        List<BusRegister> list = busRegisterService.selectBusRegisterList(busRegister, type);
        return getDataTable(list);
    }

    /**
     * 导出登录信息增删改查列表
     */
    @RequiresPermissions("bus:register:export")
    @PostMapping("/export/{type}")
    @ResponseBody
    public AjaxResult export(BusRegister busRegister, @PathVariable("type") String type)
    {
        List<BusRegister> list = busRegisterService.selectBusRegisterList(busRegister, type);
        ExcelUtil<BusRegister> util = new ExcelUtil<BusRegister>(BusRegister.class);
        return util.exportExcel(list, "register");
    }

    /**
     * 新增登录信息增删改查
     */
    @GetMapping("/add/{type}")
    public String add(@PathVariable("type") String type, ModelMap map)
    {
        map.put("type", type);
        return prefix + "/add";
    }

    /**
     * 新增保存登录信息增删改查
     */
    @RequiresPermissions("bus:register:add")
    @Log(title = "登录信息增删改查", businessType = BusinessType.INSERT)
    @PostMapping("/add/{type}")
    @ResponseBody
    public AjaxResult addSave(BusRegister busRegister, @PathVariable("type") String type)
    {
        return toAjax(busRegisterService.insertBusRegister(busRegister, type));
    }

    /**
     * 修改登录信息增删改查
     */
    @GetMapping("/edit/{type}/{id}")
    public String edit(@PathVariable("id") Long id, @PathVariable("type") String type, ModelMap mmap)
    {
        BusRegister busRegister = busRegisterService.selectBusRegisterById(id, type);
        mmap.put("busRegister", busRegister);
        mmap.put("type", type);
        return prefix + "/edit";
    }

    /**
     * 修改保存登录信息增删改查
     */
    @RequiresPermissions("bus:register:edit")
    @Log(title = "登录信息增删改查", businessType = BusinessType.UPDATE)
    @PostMapping("/edit/{type}")
    @ResponseBody
    public AjaxResult editSave(BusRegister busRegister, @PathVariable("type") String type)
    {
        return toAjax(busRegisterService.updateBusRegister(busRegister, type));
    }

    /**
     * 删除登录信息增删改查
     */
    @RequiresPermissions("bus:register:remove")
    @Log(title = "登录信息增删改查", businessType = BusinessType.DELETE)
    @PostMapping( "/remove/{type}")
    @ResponseBody
    public AjaxResult remove(String ids, @PathVariable("type") String type)
    {
        return toAjax(busRegisterService.deleteBusRegisterByIds(ids, type));
    }

    @RequiresPermissions("bus:register:view")
    @GetMapping("/importTemplate/")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BusRegister> util = new ExcelUtil<BusRegister>(BusRegister.class);
        return util.importTemplateExcel("登记数据");
    }

    @Log(title = "登记信息导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("bus:register:import")
    @PostMapping("/importData/{type}")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport, @PathVariable("type") String type) throws Exception
    {
        ExcelUtil<BusRegister> util = new ExcelUtil<BusRegister>(BusRegister.class);
        List<BusRegister> busRegisterList = util.importExcel(file.getInputStream());
        String message = busRegisterService.importUser(busRegisterList, updateSupport, type);
        return AjaxResult.success(message);
    }
}
